package com.tongjie.hotelbooking.im.conversation.repository;

import com.tongjie.hotelbooking.im.conversation.entiry.Conversation;
import com.tongjie.hotelbooking.im.conversation.entiry.MemberConversation;
import com.tongjie.hotelbooking.user.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberConversationRepository extends JpaRepository<MemberConversation, Long>, JpaSpecificationExecutor<MemberConversation> {

    List<MemberConversation> findMemberConversationsByMemberUser(User memberUser);

    @Query("select currentMemberConversation " +
            "from MemberConversation currentMemberConversation " +
            "left join Conversation  conversation on currentMemberConversation.conversation = conversation " +
            "left join MemberConversation targetMemberConversation on targetMemberConversation.conversation = conversation " +
            "where currentMemberConversation.memberUser = :currentUser " +
            "and targetMemberConversation.memberUser = :targetUser")
    MemberConversation getCurrentMemberConversationByUsers(@Param("currentUser") User currentUser, @Param("targetUser") User targetUser);

    List<MemberConversation> findMemberConversationsByConversation(Conversation conversation);

}
